const { User, Notice, Price } = require("../db");
const { Op } = require("sequelize");


exports.getNewestNotice = async (req, res) => {
    try {
        const notices = await Notice.findAll({
            ifShow: true,
        });
        res.status(200).json(notices);
    } catch (err) {
        res.status(500).json({ message: err.message });
    }
}

exports.getNoticeList = async (req, res) => {
    try {
        const { page, pageSize, ifShow = [true, false] } = req.body;
        if (page && pageSize) {
            const notices = await Notice.findAndCountAll({
                where: {
                    ifShow: ifShow
                },
                offset: pageSize * (page - 1),
                limit: pageSize,
            })
            res.status(200).json({
                data: notices.rows, total: notices.count, message: "获取成功", status: 200
            });
        }else{
            // 不分页   获取所有展示的公告
            const notices = await Notice.findAll({
                where: {
                    ifShow: ifShow
                },
            })
            res.status(200).json({
                data: notices, message: "获取成功", status: 200
            });
        }
    } catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}

exports.createNotice = async (req, res) => {
    try {
        const { title, content, ifShow } = req.body;
        const notice = await Notice.create({
            title, content, ifShow, createDate: new Date()
        })
        res.status(200).json({
            message: "创建成功", status: 200
        });
    } catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}

exports.updateNotice = async (req, res) => {
    try {
        const { id, ifShow, content, title } = req.body;
        await Notice.update({
            ifShow,
            content,
            title
        }, {
            where: {
                id
            }
        })
        res.status(200).json({
            message: "更新成功", status: 200
        });
    }
    catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}

exports.removeNotice = async (req, res) => {
    try {
        const { id } = req.body;
        const notice = await Notice.findByPk(id);
        if (!notice) {
            return res.status(404).json({ message: '公告不存在' })
        }
        await notice.destroy()
        res.status(200).json({ status: 200, message: '删除公告成功' })
    } catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}

exports.updatePrice = async (req, res) => {
    try {
        const { breakfast, lunch, dinner, id = 1, breakfastTime, lunchTime, dinnerTime, restaurantName } = req.body;
        const oldPrice = await Price.findOne({
            where: {
                id
            }
        })
        if (oldPrice) {
            await Price.update({
                breakfast, lunch, dinner,
                breakfastTime, lunchTime, dinnerTime,
                restaurantName: restaurantName
            }, {
                where: {
                    id
                }
            })
        } else {
            await Price.create({
                breakfast, lunch, dinner
            })
        }
        res.status(200).json({
            message: "更新成功", status: 200
        })
    }
    catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}

exports.getPrice = async (req, res) => {
    try {
        const price = await Price.findOrCreate({
            where: {
                id: 1
            },
            defaults: {
                breakfast: 1, lunch: 3, dinner: 3,
                breakfastTime: 8, lunchTime: '-10', dinnerTime: '-16',
                restaurantName: "食堂一"
            }
        })
        res.status(200).json({
            message: "获取成功", status: 200, data: price[0]
        })
    }
    catch (err) {
        res.status(500).json({ status: 500, message: err.message });
    }
}